﻿@using BlazorDemo.Data.Issues
@inject IssuesDataService IssuesDataService
@inject NavigationManager NavigationManager

<div class="w-100">
    <DxDataGrid DataAsync="@IssuesDataService.GetIssuesAsync"
                ShowGroupPanel="true"
                ShowFilterRow="true"
                ShowPager="true"
                HorizontalScrollBarMode="ScrollBarMode.Auto"
                PagerNavigationMode="PagerNavigationMode.InputBox"
                PagerPageSizeSelectorVisible="true"
                PagerAllDataRowsItemVisible="true"
                PagerAllowedPageSizes=@(new int[] {5,10,20})>
        <Columns>
            <DxDataGridColumn Field="@nameof(Issue.Type)" Caption=" " Width="30px" FixedStyle="DataGridFixedStyle.Left"
                              TextAlignment="DataGridTextAlign.Center" AllowFilter="false" AllowGroup="false" AllowSort="false">
                <DisplayTemplate>@GetIssueTypeIconHtml((context as Issue).Type)</DisplayTemplate>
            </DxDataGridColumn>
            <DxDataGridColumn Field="@nameof(Issue.Name)" Caption="Subject" Width="200px" FixedStyle="DataGridFixedStyle.Left">
                <DisplayTemplate>
                    @{
                        var issue = (context as Issue);
                    }
                    <button class="btn btn-link grid-btn-link" @onclick="() => GotoDetailsViewClick(issue)">@(issue.Name)</button>
                </DisplayTemplate>
            </DxDataGridColumn>
            <DxDataGridComboBoxColumn Field="@nameof(Issue.ProjectID)" DataAsync="@IssuesDataService.GetProjectsAsync" Caption="Project"
                                      Width="220px" ValueFieldName="@nameof(Project.ID)" TextFieldName="@nameof(Project.Name)">
            </DxDataGridComboBoxColumn>
            <DxDataGridComboBoxColumn Field="@nameof(Issue.CreatorID)" DataAsync="@IssuesDataService.GetUsersAsync" Caption="Owner"
                                      ValueFieldName="@nameof(User.ID)" TextFieldName="@nameof(User.FullName)" Width="120px">
            </DxDataGridComboBoxColumn>
            <DxDataGridComboBoxColumn Field="@nameof(Issue.OwnerID)" DataAsync="@IssuesDataService.GetUsersAsync" Caption="Assignee"
                                      ValueFieldName="@nameof(User.ID)" TextFieldName="@nameof(User.FullName)" Width="120px">
            </DxDataGridComboBoxColumn>
            <DxDataGridComboBoxColumn Field="@nameof(Issue.Status)" Data="@StatusList" Caption="Status"
                                      ValueFieldName="Value" TextFieldName="Text" Width="100px">
            </DxDataGridComboBoxColumn>
            <DxDataGridDateEditColumn Field="@nameof(Issue.CreatedDate)" Caption="Created" Width="80px">
            </DxDataGridDateEditColumn>
            <DxDataGridDateEditColumn Field="@nameof(Issue.ModifiedDate)" Caption="Modified" Width="80px">
            </DxDataGridDateEditColumn>
            <DxDataGridDateEditColumn Field="@nameof(Issue.FixedDate)" Caption="Fixed" Width="80px">
            </DxDataGridDateEditColumn>
            <DxDataGridColumn Field="@nameof(Issue.Priority)" Caption=" " Width="60px" TextAlignment="DataGridTextAlign.Center"
                              AllowFilter="false" AllowGroup="false" AllowSort="false">
                <DisplayTemplate>@GetIssuePriorityIconHtml((context as Issue).Priority)</DisplayTemplate>
            </DxDataGridColumn>
        </Columns>
        <GroupSummary>
            <DxDataGridSummaryItem Field="@nameof(Issue.ID)" SummaryType="SummaryItemType.Count"
                                   ShowInColumn="@nameof(Issue.ProjectID)" DisplayFormat="Count is {0}" />
        </GroupSummary>
        <TotalSummary>
            <DxDataGridSummaryItem Field="@nameof(Issue.ID)" SummaryType="SummaryItemType.Count"
                                   ShowInColumn="@nameof(Issue.Name)" />
        </TotalSummary>
    </DxDataGrid>
</div>

@code {
    static readonly RenderFragment EmptyHtml = _ => {};
    static IEnumerable<object> StatusList { get; set; } = ((IssueStatus[])Enum.GetValues(typeof(IssueStatus)))
        .Select(s => new { Text = Enum.GetName(typeof(IssueStatus), s), Value = s });

    [Parameter]
    public EventCallback<Issue> GotoDetailsView { get; set; }

    async Task GotoDetailsViewClick(Issue issue) {
        await GotoDetailsView.InvokeAsync(issue);
    }

    public RenderFragment GetIssuePriorityIconHtml(IssuePriority priority) {
        var prioritySuffix = GetPriorityCssClassPostfix(priority);
        return @<span class=@($"badge bg-{prioritySuffix} py-1 px-2") 
                        title=@($"{priority} Priority")> 
                            @priority 
                        </span>;
    }
    public RenderFragment GetIssueTypeIconHtml(IssueType type) =>
        type switch {
           IssueType.Bug => @<span class='badge bg-danger py-1 px-2' title='Bug'>B</span>,
            _ => EmptyHtml
        };
    static string GetPriorityCssClassPostfix(IssuePriority priority) =>
        priority switch {
            IssuePriority.High => "danger",
            IssuePriority.Low => "info",
            _ => "warning"
        };
}
